package com.sry.blog.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sry.blog.sys.mapper.ArticleMapper;
import com.sry.blog.sys.model.Article;
import com.sry.blog.sys.service.ArticleService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB; (`author`) REFER `blog/user`(`id`) ON UPDATE CASCADE 服务实现类
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    /**
     * 创建或修改文章
     *
     * @param article 文章
     * @return boolean
     */
    @Override
    public boolean creatOrUpdate(Article article) {
        return saveOrUpdate(article);
    }

    /**
     * 通过id获取
     *
     * @param id id
     * @return 文章
     */
    @Override
    public Article getOneById(Integer id) {
        return getById(id);
    }

    /**
     * 通过文章属性获取
     *
     * @param article 文章
     * @return 文章
     */
    @Override
    public Article getOne(Article article) {
        final Integer author = article.getAuthor();
        final String title = article.getTitle();
        final String content = article.getContent();
        final String introduce = article.getIntroduce();
        final Date publishTime = article.getPublishTime();
        final Date recentEdit = article.getRecentEdit();
        final Integer status = article.getStatus();
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Article> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(title)) {
            lambda.eq(Article::getTitle, title);
        }
        if (null != author) {
            lambda.eq(Article::getAuthor, author);
        }
        if (null != status) {
            lambda.eq(Article::getStatus, status);
        }
        if (StrUtil.isNotEmpty(content)) {
            lambda.eq(Article::getContent, content);
        }
        if (StrUtil.isNotEmpty(introduce)) {
            lambda.eq(Article::getIntroduce, introduce);
        }
        if (null != publishTime) {
            lambda.eq(Article::getPublishTime, publishTime);
        }
        if (null != recentEdit) {
            lambda.eq(Article::getRecentEdit, recentEdit);
        }
        return getOne(wrapper);
    }

    /**
     * 条件分页查询
     *
     * @param article  文章
     * @param pageSize 页面大小
     * @param pageNum  页数
     * @return page
     */
    @Override
    public List<Article> list(Article article, Integer pageNum, Integer pageSize) {
        final Integer author = article.getAuthor();
        final String title = article.getTitle();
        final String content = article.getContent();
        final String introduce = article.getIntroduce();
        final Date publishTime = article.getPublishTime();
        final Date recentEdit = article.getRecentEdit();
        final Integer status = article.getStatus();
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Article> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(title)) {
            lambda.like(Article::getTitle, title);
        }
        if (null != author) {
            lambda.or().eq(Article::getAuthor, author);
        }
        if (StrUtil.isNotEmpty(content)) {
            lambda.or().like(Article::getContent, content);
        }
        if (StrUtil.isNotEmpty(introduce)) {
            lambda.or().like(Article::getIntroduce, introduce);
        }
        if (null != publishTime) {
            lambda.or().eq(Article::getPublishTime, publishTime);
        }
        if (null != recentEdit) {
            lambda.or().eq(Article::getRecentEdit, recentEdit);
        }
        lambda.last("LIMIT " + (pageNum - 1) * pageSize + "," + pageSize);
        return list(wrapper);
    }

    /**
     * 分页查询
     *
     * @param pageSize 页面大小
     * @param pageNum  页数
     * @return page
     */
    @Override
    public Page<Article> list(Integer pageNum, Integer pageSize) {
        Page<Article> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Article> lambda = wrapper.lambda();
        return page(page, wrapper);

    }


    @Override
    public boolean updateStatus(Article article) {
        return updateById(article);
    }

    @Override
    public void readCountIncrease(Integer id) {
        Article article = getById(id);
        article.setReadCount(article.getReadCount() + 1);
        updateById(article);
    }

    @Override
    public List<Article> getRecentArticle() {
        //取前五个
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("recent_edit");
        wrapper.last("limit 5");
        wrapper.ne("status", 0);
        return list(wrapper);
    }

    @Override
    public List<Article> getHotArticle() {
        //取前五个
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("read_count");
        wrapper.last("limit 5");
        return list(wrapper);
    }

    @Override
    public List<Article> list(Article article) {
        final Integer author = article.getAuthor();
        final String title = article.getTitle();
        final String content = article.getContent();
        final String introduce = article.getIntroduce();
        final Date publishTime = article.getPublishTime();
        final Date recentEdit = article.getRecentEdit();
        final Integer status = article.getStatus();
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Article> lambda = wrapper.lambda();
        wrapper.orderByDesc("publish_time");
        if (StrUtil.isNotEmpty(title)) {
            lambda.like(Article::getTitle, title);
        }
        if (null != author) {
            lambda.or().eq(Article::getAuthor, author);
        }
        if (StrUtil.isNotEmpty(content)) {
            lambda.or().like(Article::getContent, content);
        }
        if (StrUtil.isNotEmpty(introduce)) {
            lambda.or().like(Article::getIntroduce, introduce);
        }
        if (null != publishTime) {
            lambda.or().eq(Article::getPublishTime, publishTime);
        }
        if (null != recentEdit) {
            lambda.or().eq(Article::getRecentEdit, recentEdit);
        }
        return list(wrapper);
    }

    @Override
    public long count(Article article) {
        final Integer author = article.getAuthor();
        final String title = article.getTitle();
        final String content = article.getContent();
        final String introduce = article.getIntroduce();
        final Date publishTime = article.getPublishTime();
        final Date recentEdit = article.getRecentEdit();
        final Integer status = article.getStatus();
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Article> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(title)) {
            lambda.like(Article::getTitle, title);
        }
        if (null != author) {
            lambda.or().eq(Article::getAuthor, author);
        }
        if (StrUtil.isNotEmpty(content)) {
            lambda.or().like(Article::getContent, content);
        }
        if (StrUtil.isNotEmpty(introduce)) {
            lambda.or().like(Article::getIntroduce, introduce);
        }
        if (null != publishTime) {
            lambda.or().eq(Article::getPublishTime, publishTime);
        }
        if (null != recentEdit) {
            lambda.or().eq(Article::getRecentEdit, recentEdit);
        }
        return count(wrapper);
    }


}
